//自主实现无头双向链表
public class MyLinkedList {
    static class ListNode {
        public int val;

        public ListNode next;

        public ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }
    ListNode head;

    ListNode last;
    //头插法
    public void addFirst(int data){
        ListNode newNode = new ListNode(data);
        if(head == null) {
            head = newNode;
            last = newNode;
            return;
        }
        newNode.next = head;
        head.prev = newNode;
        head = newNode;
    }
    //尾插法
    public void addLast(int data){
        ListNode newNode = new ListNode(data);
        if(head == null) {
            head = newNode;
            last = newNode;
            return;
        }
        last.next = newNode;
        newNode.prev = last;
        last = newNode;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNode newNode = new ListNode(data);
        if(index > size() || index < 0) {
            throw new IndexOutOfBoundsException("双向链表index位置不合法！"+ index);
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        newNode.prev = cur.prev;
        newNode.next = cur;
        cur.prev.next = newNode;
        cur.prev = newNode;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head == null) {
            return false;
        }
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    //若此时链表之中仅存在一个元素时。如若删除之后无第二个元素时，会产生空指针异常，只有有第二个元素时，我们才可以将新的元素的
                    //prev设为null
                    if (head != null){
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else if (cur == last) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
        System.out.println("链表中并不存在" + key);
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    //若此时链表之中仅存在一个元素时。如若删除之后无第二个元素时，会产生空指针异常，只有有第二个元素时，我们才可以将新的元素的
                    //prev设为null
                    if (head != null){
                        head.prev = null;
                    } else {
                        last = null;
                    }
                } else if (cur == last) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int length = 0;
        ListNode cur = head;
        while(cur != null) {
            length++;
            cur = cur.next;
        }
        return length;
    }
    public void display(){
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    public void clear(){
        ListNode cur = head;
        while(cur != null) {
            ListNode next = cur.next;
            cur.next = null;
            cur.prev = null;
            cur = next;
        }
        head = null;
        last = null;
    }

    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addLast(10);
        list.addLast(20);
        list.addLast(30);
        list.addLast(30);
        list.addLast(30);
        list.addIndex(1,99);
        list.addFirst(1);
        list.addLast(30);
        list.addFirst(2);
        list.addFirst(3);
        list.addLast(30);
        list.addLast(30);
        list.display();
        System.out.println();
        list.removeAllKey(30);
        list.display();
    }
}